package org.openurp.edu.student.info.model

/**
 * @author xinzhou
 */
object HanZiStyle {

  /**
   * Constant <code>MAX=99999</code>
   */
  val MAX = 99999

  /**
   * Constant <code>CHINESE_NAMES="{ 零, 壹, 贰, 叁, 肆, 伍, 陆, 柒, 捌, 玖, 拾 }"</code>
   */
  val CHINESE_NAMES = Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾")

  /**
   * Constant <code>PRIORITIES="{ 拾, 佰, 仟, 万 }"</code>
   */
  val PRIORITIES = Array("拾", "佰", "仟", "万")

  /**
   * {@inheritDoc}
   */
  def build(seq: Int): String = {
    if (seq > MAX) {
      throw new RuntimeException("seq greate than " + MAX)
    }
    buildText(String.valueOf(seq))
  }

  /**
   * <p>
   * buildText.
   * </p>
   *
   * @param str1 a {@link java.lang.String} object.
   * @return a {@link java.lang.String} object.
   */
  def buildText(str1: String): String = {
    val sb = new StringBuilder()
    for (i <- 0 until str1.length) {
      val numChar = str1.charAt(i)
      var temp = basicOf(numChar - '0')
      if (numChar - '0' > 0) {
        temp = temp + priorityOf(str1.length - i)
      }
      sb.append(temp)
    }
    var result = sb.toString
    result = result.replaceAll("零一十", "零十")
    result = result.replaceAll("零零", "零")
    if (result.endsWith("零")) {
      result = result.substring(0, result.length() - 1)
    }

    result
  }

  /**
   * <p>
   * basicOf.
   * </p>
   *
   * @param num a int.
   * @return a {@link java.lang.String} object.
   */
  def basicOf(num: Int): String = CHINESE_NAMES(num)

  /**
   * <p>
   * priorityOf.
   * </p>
   *
   * @param index a int.
   * @return a {@link java.lang.String} object.
   */
  def priorityOf(index: Int): String = {
    if (index < 2) {
      ""
    } else {
      PRIORITIES(index - 2)
    }
  }

}